﻿using System;
using System.Collections.Generic;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;
using ZLDC.Topo.Model.Estate;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ZLDC.Topo.BLL.Estate
{
                public class EstateBLL
    {
                                public static string EstateSave(int id, string name, string roadName, string otherName, string namePy, string otherNamePy, int cityId, string cityName, int areaId, string areaName, string address, string usage, string type, float constructionArea, string construction, float salesFare, int familyTotal, string propertyLevel, string propertyCompany, string propertyTel, float propertyFare, string realestateName, string realestateTel, int registerUserid, string registerName, int registerDeptid, string registerDept, string updateName, string aroundCondition, string trafficCondition, string remark, string completeDate, int isvalid, string leaseTime, string park)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.EstateDAL.p_estate_save(id, name, roadName, otherName, namePy, otherNamePy, cityId, cityName, areaId, areaName, address, usage, type, constructionArea, construction, familyTotal,
                    propertyLevel, propertyCompany, propertyTel, propertyFare, salesFare, realestateName, realestateTel, registerUserid, registerName, registerDeptid, registerDept, updateName, aroundCondition,
                    trafficCondition, remark, completeDate, isvalid, leaseTime, park, out outmsg, out newid, db);
            }

            return outmsg;
        }
                               
        public static string EstateSaveAndBuildName(int id, string name, string roadName, string otherName, string namePy, string otherNamePy, int cityId, string cityName, int areaId, string areaName, string address, string usage, string type, float constructionArea, string construction, float salesFare, int familyTotal, string propertyLevel, string propertyCompany, string propertyTel, float propertyFare, string realestateName, string realestateTel, int registerUserid, string registerName, int registerDeptid, string registerDept, string updateName, string aroundCondition, string trafficCondition, string remark, string completeDate, int isvalid, string leaseTime, string park)
        {
            string outmsg = "";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                ZLDC.DAL.Estate.EstateDAL.p_estate_save(id, name, roadName, otherName, namePy, otherNamePy, cityId, cityName, areaId, areaName, address, usage, type, constructionArea, construction, familyTotal,
                    propertyLevel, propertyCompany, propertyTel, propertyFare, salesFare, realestateName, realestateTel, registerUserid, registerName, registerDeptid, registerDept, updateName, aroundCondition,
                    trafficCondition, remark, completeDate, isvalid, leaseTime, park, out outmsg, out newid, db);
            }

            return newid+","+outmsg;
        }

                                        public static string EstateList(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.EstateDAL.p_estate_list_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
            _Json json = new _Json(dt, 1);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }
         
                                        public static string EstateList(int userid, int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0;                       int pageall = 0;         
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.EstateDAL.p_estate_list_action_table(userid, page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);      
                        string field = UserOptionBLL.p_user_option_detail(3, userid);

            _Json json = new _Json(dt, 1);
            return "{" + field + ",page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }    

                                                                public static string EstateAutoList(int top, string condition)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.EstateDAL.p_estate_list_table(top, condition, db);
            }

            _Json json = new _Json(dt, 1);
            return "{record:" + json.ToJson() + "}";
        }       

                                                public static string EstateDetail(int id)
        {
            DataTable dt = null;

            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.EstateDAL.p_estate_detail_table(id, db);
            }

            _Json json = new _Json(dt, 1);
            return "{record:" + json.ToJson() + "}";
        }

                                                public static EstateInfo EstateDetailById(int id)
        {
            using (_Database db = new _Database("estate"))
            {
                return ZLDC.DAL.Estate.EstateDAL.p_estate_detail(id, db);
            }
        }

                                                public static string EstateNameById(int id)
        {
            return EstateDetailById(id).Name;
        }

                                                public static string EstateCheck(int id)
        {
            string outmsg = "审核失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.EstateDAL.EstateCheck(id, db);
                if (rows > 0)
                    outmsg = "审核成功";
            }

            return outmsg;
        }

                                                        public static string EstateCheck(int id, int companyId)
        {
            string outmsg = "审核失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                if (companyId == 1)
                    rows = ZLDC.DAL.Estate.EstateDAL.EstateCheck(id, db);
                else
                    rows = ZLDC.DAL.Estate.EstateDAL.EstateCheck_CS(id, db);
                if (rows > 0)
                    outmsg = "审核成功";
            }

            return outmsg;
        }

        public static string EstateCheck(int id, int isvalid, string Approval_views)
        {
            string outmsg = "审核失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.EstateDAL.EstateCheck(id,isvalid,Approval_views, db);
                if (rows > 0)
                    outmsg = "审核成功";
            }

            return outmsg;
        }

                                                public static string EstateCheckAll(int eid)
        {
            string outmsg = "审核失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.EstateDAL.EstateCheckAll(eid, db);
                if (rows > 0)
                    outmsg = "审核成功";
            }

            return outmsg;
        }


                                                public static string EstateDel(int id)
        {
            string outmsg = "删除失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.EstateDAL.EstateDel(id, db);
                if (rows > 0)
                    outmsg = "删除成功";
            }

            return outmsg;
        }

                                                public static string EstateDelAll(int eid)
        {
            string outmsg = "删除失败";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.EstateDAL.EstateDelAll(eid, db);
                if (rows > 0)
                    outmsg = "删除成功";
            }

            return outmsg;
        }

        public static string EstateDelAndIsValid(int id) 
        {
            string outmsg = "您无限删除有效楼盘！";
            int rows = 0;

            using (_Database db = new _Database("estate"))
            {
                rows = ZLDC.DAL.Estate.EstateDAL.EstateDelAndIsValid(id, db);
                if (rows > 0)
                    outmsg = "删除成功！";
            }

            return outmsg;
        }
                                                                        
                                                
                
                                                                                                public static int UpdateOtherNameById(string id, string oname, string type, string tablename, string commitName, string commitDept, string commitDeptId)
        {
            return ZLDC.DAL.Estate.EstateDAL.UpdateOtherNameById(id, oname, type, tablename, commitName, commitDept, commitDeptId);
        }
        
                                                public string SelectNameByEid(int id)
        {
            return ZLDC.DAL.Estate.EstateDAL.SelectNameByEid(id);
        }

        public static string SelectIdByName(string NAME)
        {
            return ZLDC.DAL.Estate.EstateDAL.SelectIdByName(NAME);
        }

                public static string DeleteEstate(int id)
        {
            string outmsg = "删除失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.EstateDAL.DeleteEstate(id, db);
            }

            if (newid > 0)
                outmsg = "删除成功";

            return outmsg;
        }

                public static string UpdateDltFlag(int id)
        {
            string outmsg = "恢复失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.EstateDAL.UpdateDelFlag(id, db);
            }

            if (newid > 0)
                outmsg = "恢复成功";

            return outmsg;
        }

                                                public static string GetAreaByDeptId(int deptid)
        {
            string condition = "";
            ZLDC.Topo.Model.Estate.AuthorizationInfo info = ZLDC.Topo.BLL.Estate.AuthorizationBLL.p_authorization_detail(deptid);

            if (info == null || info.ID <= 0)
                return "";

                        string areas = "";
            string lastChar = "";
            if (info.Areas != "")
            {
                                lastChar = info.Areas.Substring(info.Areas.Length - 1, 1);
                if (lastChar.Contains(","))
                    areas = info.Areas.Substring(0, info.Areas.Length - 1);
                else
                    areas = info.Areas;
            }

                        string estates = "";
            if (info.Estates != "")
            {
                                lastChar = info.Estates.Substring(info.Estates.Length - 1, 1);
                if (lastChar.Contains(","))
                    estates = info.Estates.Substring(0, info.Estates.Length - 1);
                else
                    estates = info.Estates;
            }

                        string usage = "";
            if (info.Useage != "")
            {
                                lastChar = info.Useage.Substring(info.Useage.Length - 1, 1);
                if (lastChar.Contains(","))
                    usage = info.Useage.Substring(0, info.Useage.Length - 1);
                else
                    usage = info.Useage;
            }

            if (!areas.Equals(""))
                condition += " (areaid in(" + areas + ")";

            if (condition != "")
            {
                if (!estates.Equals(""))
                    condition += " or id in(" + estates + "))";
                else
                    condition += ")";
            }
            else
            {
                if (!estates.Equals(""))
                    condition += " id in(" + estates + ")";
            }

            if (condition != "")
            {
                if (!usage.Equals(""))
                    condition += " and  CHARINDEX([usage], '" + usage + "') > 0";
            }
            else
            {
                if (!usage.Equals(""))
                    condition += " CHARINDEX([usage], '" + usage + "') > 0";
            }

            return condition;
        }

                                                        public static string UpateEstate(int id)
        {
            string outmsg = "确认失败";
            int newid = 0;

            using (_Database db = new _Database("estate"))
            {
                newid = ZLDC.DAL.Estate.EstateDAL.UpateEstate(id, db);
            }

            if (newid > 0)
                outmsg = "确认成功";

            return outmsg;
        }

    }

}
